﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReportTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string Printer = "Microsoft Print to PDF";

        /// <summary>
        /// 图片转字节流
        /// </summary>
        /// <param name="Picture"></param>
        /// <returns></returns>
        private byte[] ImageToByte(Image Picture)
        {
            MemoryStream ms = new MemoryStream();
            if (Picture == null) return new byte[ms.Length];
            Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] BPicture = new byte[ms.Length];
            BPicture = ms.GetBuffer();
            return BPicture;
        }

        /// <summary>
        /// 1.直接传参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            FastReport.Report report = new FastReport.Report();
            report.Load("ReportFile\\1.直接传参数.frx");
            report.PrintSettings.Printer = Printer;
            string Username = "直接传递参数";
            report.SetParameterValue("prams1", Username);
            report.Show();
        }

        /// <summary>
        /// 2.DateSet传参
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            FastReport.Report report = new FastReport.Report();
            report.Load("ReportFile\\2.DateSet传参.frx");
            report.PrintSettings.Printer = Printer;
            DataSet dt = new DataSet();
            DataTable tb = new DataTable();
            tb.TableName = "成绩表";
            tb.Columns.Add(new DataColumn("语文", Type.GetType("System.Int32")));
            tb.Columns.Add(new DataColumn("数学", Type.GetType("System.Int32")));
            tb.Columns.Add(new DataColumn("英语", Type.GetType("System.Int32")));
            for (int i = 0; i < 10; i++)
            {
                DataRow dr = tb.NewRow();
                dr["语文"] = 36;
                dr["数学"] = 89;
                dr["英语"] = 82;
                tb.Rows.Add(dr);
            }
            dt.Tables.Add(tb);
            report.RegisterData(dt, "Data");
            report.Show();
        }

        /// <summary>
        /// 3.图片传输
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            FastReport.Report report = new FastReport.Report();
            report.PrintSettings.Printer = Printer;
            report.Load("ReportFile\\3.图片传输.frx");
            //直接传图
            Image image = Image.FromFile("test.jpg");
            FastReport.PictureObject imgobj = report.FindObject("Picture1") as FastReport.PictureObject;
            imgobj.Width = 120;
            imgobj.Height = 120;
            imgobj.Image = image;
            //DateSet传图
            byte[] img = ImageToByte(image);
            DataSet dt = new DataSet();
            DataTable tb = new DataTable();
            tb.TableName = "图片表";
            tb.Columns.Add(new DataColumn("图片1", Type.GetType("System.Byte[]")));
            DataRow dr = tb.NewRow();
            dr["图片1"] = img;
            tb.Rows.Add(dr);
            dt.Tables.Add(tb);
            report.RegisterData(dt, "Data");
            report.Show();
        }
    }
}
